package com.xcfw.dorm.controller.dormcenter;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.*;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.seviceimpl.dormcenter.StudentSeriviceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @program: Dorm
 * @description: 学生
 * @author: Bruse Queen
 * @create: 2018-04-07 16:43
 **/
@RestController
@Slf4j
@RequestMapping(value = "/dormcenter")
@CrossOrigin
public class StudentController extends BaseController {
    @Autowired
    @Qualifier("studentServiceImpl")
    private ZSGCService zsgcService;
    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public ReturnJson addStudent(@RequestBody Student student) {
        try {
            zsgcService.add(student);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "学生信息新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("StudentController---addStudent--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteStudent", method = RequestMethod.POST)
    public ReturnJson deleteStudent(@RequestBody Student student) {
        try {
            zsgcService.delete(student.getId());
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "学生信息删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("StudentController---deleteStudent--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateStudent", method = RequestMethod.POST)
    public ReturnJson updateStudent(@RequestBody Student student) {
        try {
            zsgcService.update(student);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "学生信息修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("StudentController---updateStudent--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectStudent", method = RequestMethod.POST)
    public ReturnJson selectStudent(@RequestBody Student student) {
        try {
            result = (ReturnJson) zsgcService.select(student);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("StudentController---selectStudent--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
